package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Topics;

public class GenericTopicsDAO extends AutoKeys implements
		org.jetnuke.dao.TopicsDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericTopicsDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#retrieveByPK(int)
	 */
	public Topics retrieveByPK(int topicId) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("TopicModel2.selectByID"));
		p.setInt(1, topicId);

		ResultSet rs = p.executeQuery();
		Topics t = new Topics();

		if (rs.next()) {
			fillTopicsFromResultSet(t, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return t;
	}

	/**
	 * @param t
	 * @param rs
	 */
	protected void fillTopicsFromResultSet(Topics t, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillTopicsFromResultSet(Topics, ResultSet) - start"); //$NON-NLS-1$
		}

		t.setCOUNTER(rs.getInt("COUNTER"));
		t.setTOPICID(rs.getInt("TOPICID"));
		t.setTOPICIMAGE(rs.getString("TOPICIMAGE"));
		t.setTOPICNAME(rs.getString("TOPICIMAGE"));
		t.setTOPICTEXT(rs.getString("TOPICTEXT"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillTopicsFromResultSet(Topics, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#addNew(net.jforum.entities.Topic)
	 */
	public int addNew(Topics top) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Topics) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("TopicModel2.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("TopicModel2.lastGeneratedTopicId"));
		initNewTopic(top, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Topics) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param top
	 * @param p
	 */
	private void initNewTopic(Topics top, PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewTopic(Topics, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, top.getTOPICNAME());
		p.setString(2, top.getTOPICIMAGE());
		p.setString(3, top.getTOPICTEXT());
		p.setInt(4, 0);

		if (logger.isDebugEnabled()) {
			logger.debug("initNewTopic(Topics, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#update(org.jetnuke.entities.Topics)
	 */
	public void update(Topics top) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Topics) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("TopicModel2.update"));
		p.setString(1, top.getTOPICNAME());
		p.setString(2, top.getTOPICIMAGE());
		p.setString(3, top.getTOPICTEXT());
		p.setInt(4, top.getCOUNTER());
		p.setInt(5, top.getTOPICID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Topics) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#selectAllOrderTopicName()
	 */
	public List selectAllOrderTopicName() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTopicName() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("TopicModel2.selectAllOrderTopicName"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTopicName() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Topics u = new Topics();
			fillTopicsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("TopicModel2.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.TopicsModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("TopicModel2.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

}
